package com.baidu.manager.modules.sys.controller;

import com.baidu.manager.common.annotation.SysLog;
import com.baidu.manager.common.exception.JqlException;
import com.baidu.manager.common.utils.PageUtils;
import com.baidu.manager.common.utils.Query;
import com.baidu.manager.common.utils.Result;
import com.baidu.manager.common.validator.ValidatorUtils;
import com.baidu.manager.common.validator.group.AddGroup;
import com.baidu.manager.common.validator.group.UpdateGroup;
import com.baidu.manager.modules.sys.entity.SysUser;
import com.baidu.manager.modules.sys.service.SysUserRoleService;
import com.baidu.manager.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: jql
 * @description: TODO()
 * @Date: Created by 14:36 2018/4/13
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController{

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 所有用户列表
     * @param params
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:user:list")
    public Result findUserList(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<SysUser> userList = sysUserService.queryList(query);
        int total = sysUserService.queryTotal(query);

        PageUtils pageUtils = new PageUtils(userList, total, query.getLimit(), query.getPage());

        return Result.ok().put("page",pageUtils);
    }

    /**
     * 获取登录用户信息
     * @return
     */
   @GetMapping("/info")
   public Result info(){
        return Result.ok().put("user",getUser());
   }

    /**
     * 用户信息
     * @param userId
     * @return
     */
    @GetMapping("/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public Result info(@PathVariable("userId") Long userId){
        SysUser user = sysUserService.queryObject(userId);

        //获取用户所属的角色列表
        List<Long> roleList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleList);
        return Result.ok().put("user", user);
    }

    /**
     * 修改密码
     * @param password
     * @param newPassword
     * @return
     */
    @SysLog(value="修改密码")
    @RequestMapping("/updatePassword")
    public Result updatePassword(String password, String newPassword){
        if(StringUtils.isNotBlank(newPassword)){
            throw new JqlException("心密码不为空");
        }
        //sha256加密
        password = new Sha256Hash(password, getUser().getSalt()).toHex();
        //sha256加密
        newPassword = new Sha256Hash(newPassword, getUser().getSalt()).toHex();
        //更新密码
//        int count = sysUserService.updatePassword(getUser(), password, newPassword);

        return null;

    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @SysLog(value="添加用户")
    @RequiresPermissions("user:save")
    @PostMapping
    public Result save(@RequestBody SysUser user){
    ValidatorUtils.validateEntity(user, AddGroup.class);
    sysUserService.save(user);
    return Result.ok();
}
    @RequestMapping("/test001")
    public void test001(){
        System.out.print(111);
    }

    public Result update(@RequestBody SysUser user){
        ValidatorUtils.validateEntity(user, UpdateGroup.class);
        sysUserService.update(user);
        return Result.ok();
    }
}
